package http

import (
	"context"
	"log"
	"net"
	"net/http"
	"time"
)

type Server struct {
	*http.Server
	port string
}

func (s *Server) Start() error {
	l, err := net.Listen("tcp", "0.0.0.0:"+s.port)
	if err != nil {
		return err
	}
	log.Default().Println("http server listening on: " + s.port)
	return s.Serve(l)
}

func (s *Server) Stop(ctx context.Context) {
	log.Default().Println("http server stopping on: " + s.port)
	s.Shutdown(ctx)
}

func (s *Server) ServeHTTP(r http.ResponseWriter, h *http.Request) {
	// 处理请求
	log.Default().Printf("%s %s", h.Method ,h.RequestURI)
	time.Sleep(time.Second)
}

func NewServer(port string) *Server {
	return &Server{
		port: port,
		Server: &http.Server{
			Handler: &Server{},
		},
	}
}
